package com.defysoft.waukids.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.defysoft.waukids.Custom_Data_Type.Bc_Student_Cus;
import com.defysoft.waukids.PublicConstant;
import com.defysoft.waukids.R;
import com.defysoft.waukids.intro.Intro_Main_A;

public class Adapter_IntroSelectStudent_L extends ArrayAdapter<String> {
	ArrayList<Bc_Student_Cus> list;
	LayoutInflater layoutInflater;
	SharedPreferences.Editor editor; // 학생정보 여기서 넣기. student.edit();

	public Adapter_IntroSelectStudent_L(Context context, int resource,
			ArrayList<Bc_Student_Cus> list, LayoutInflater layoutInflater,
			SharedPreferences.Editor editor) {
		super(context, resource);
		this.list = list;
		this.layoutInflater = layoutInflater;
		this.editor = editor;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Holder holder;
		if (convertView == null) {
			holder = new Holder();
			convertView = layoutInflater.inflate(R.layout.select_student_row,
					null);
			holder.tv1 = (TextView) convertView
					.findViewById(R.id.SELECT_STUDENT_NAME);
			holder.tv2 = (TextView) convertView
					.findViewById(R.id.SELECT_STUDENT_CLASS);
			holder.tv3 = (TextView) convertView
					.findViewById(R.id.SELECT_STUDENT_BIRTH);
			holder.iv = (WebView) convertView
					.findViewById(R.id.SELECT_STUDENT_IMG);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		holder.tv1.setText(list.get(position).Stu_Name + "어린이");
		holder.tv2.setText(list.get(position).classname);
		holder.tv3.setText(list.get(position).Stu_Birth);
		final int pos = position;
		holder.iv.setVerticalScrollBarEnabled(false);
		holder.iv.setVerticalScrollbarOverlay(false);
		holder.iv.setHorizontalScrollBarEnabled(false);
		holder.iv.setHorizontalScrollbarOverlay(false);
		holder.iv.setInitialScale(100);

		if ( list.get(pos).Stu_Photo == null||list.get(pos).Stu_Photo.length()<5) {// 이미지 파일이 없으면 앱내에서 지정된 이미지를 뿌려주고
			holder.iv.setBackgroundColor(0);
			holder.iv.setBackgroundResource(R.drawable.perm_group_personal_info);
		} else {// 이미지 파일이 있으면
			holder.iv.loadDataWithBaseURL(null,
					creHtmlBody(PublicConstant.URI_STUDENDPHOTO
							+ list.get(pos).Stu_Photo), "text/html", "utf-8", null);
		}
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) { // 어린이를 선택하면. 학생정보 저장.

				editor.putString("PK_Stu_Code", list.get(pos).PK_Stu_Code + "");
				editor.putString("FK_Sch_Stu_Code",
						list.get(pos).FK_Sch_Stu_Code + "");
				editor.putString("FK_Cla_Stu_Code",
						list.get(pos).FK_Cla_Stu_Code + "");
				editor.putString("FK_Bea_Stu_Code",
						list.get(pos).FK_Bea_Stu_Code + "");
				editor.putString("Stu_Name", list.get(pos).Stu_Name + "");
				editor.putString("Stu_Birth", list.get(pos).Stu_Birth + "");
				editor.putString("Stu_Info", list.get(pos).Stu_Info + "");
				editor.putString("Stu_Photo", list.get(pos).Stu_Photo + "");
				editor.commit();

				getContext().startActivity(
						new Intent(getContext(), Intro_Main_A.class));

			}
		});
		return convertView;
	}

	class Holder {
		TextView tv1;
		TextView tv2;
		TextView tv3;
		WebView iv;
	}

	public String creHtmlBody(String imagUrl) {
		StringBuffer sb = new StringBuffer("<HTML>");
		sb.append("<HEAD>");
		sb.append("</HEAD>");
		sb.append("<BODY  style='margin: 0; padding: 0'>");
		sb.append("<img width=\"100%\" height=\"100%\" src=\"" + imagUrl
				+ "\">");
		sb.append("</BODY>");
		sb.append("</HTML>");

		return sb.toString();
	}
}
